
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()

	self.Entity:SetModel( "models/\props_phx/amraam.mdl" )
	self.Entity:SetName("Missile")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	util.PrecacheSound("ambient/explosions/explode_1.wav")

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end

    	self.Entity:SetKeyValue("rendercolor", "255 255 255")
	self.PhysObj = self.Entity:GetPhysicsObject()
	self.STime = CurTime()
	self.LTime = self.STime + self.ParL.LTime


end

function ENT:PhysicsUpdate()

	if(self.Exploded) then
		self.Entity:Remove()
		return
	end


	if(self.FuelAmount > 0) then
		local vectorMoved = self.Entity:GetPos() - self.LastPosition
		local amountMoved = vectorMoved:Length()
		self.FuelAmount = self.FuelAmount - amountMoved	

		if (self.Locked == true) then
			self.Target = Vector(self.XCo, self.YCo, self.ZCo)
			AimVec = ( self.Target - self.LastPosition ):Angle()
			self.Entity:SetAngles( AimVec )
		end
	
		self.PhysObj:SetVelocity(self.Entity:GetForward()*3100)

		if(self.FuelAmount < 0) then
			local phys = self.Entity:GetPhysicsObject()
			phys:EnableGravity(true)
			phys:EnableDrag(true)
			self.Locked = false
		end

		self.LastPosition = self.Entity:GetPos()
	end
end

function ENT:Think()
	
	if (self.PreLaunch == false) then
		self.PreLaunch = true
		local phys = self.Entity:GetPhysicsObject()
		if (phys:IsValid()) then
			phys:Wake()
			phys:EnableGravity(false)
			phys:EnableDrag(false)
			phys:EnableCollisions(true)
			phys:EnableMotion(true)
		end

		FieryTrailOfPainfullDoomAndDestruction = ents.Create("env_fire_trail")

		self.LastPosition = self.Entity:GetPos()

		FieryTrailOfPainfullDoomAndDestruction:SetAngles( self.Entity:GetAngles()  )
		FieryTrailOfPainfullDoomAndDestruction:SetPos( self.Entity:GetPos() )

		FieryTrailOfPainfullDoomAndDestruction:SetParent(self.Entity)
		FieryTrailOfPainfullDoomAndDestruction:Spawn()
		FieryTrailOfPainfullDoomAndDestruction:Activate()
		self.PhysObj:SetVelocity(self.Entity:GetForward()*3100)

		local SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction = ents.Create("env_smoketrail") //Sorry, I coulden't resist.
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("startsize","8")
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("endsize","18")
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("minspeed","1")			SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("maxspeed","2")
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("startcolor","40 40 40")
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("endcolor","40 40 40")
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("opacity",".3")
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("spawnrate","60")  //Makes smoke thick.
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetKeyValue("lifetime","0.6")  //We don't want the smoke to be there for a long time.
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetPos(self.Entity:GetPos())
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:Spawn()
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:Fire("kill","",10)
		SmokeyTrailofThickBlackSmokefromFieryTrailOfPainfullDoomAndDestruction:SetParent(self.Entity)	

		local smoke = ents.Create("env_smoketrail") //Lesser smoke, not as thick and as black.
		smoke:SetKeyValue("startsize","30")
		smoke:SetKeyValue("endsize","60")
		smoke:SetKeyValue("minspeed","10")
		smoke:SetKeyValue("startcolor","0 0 0")
		smoke:SetKeyValue("endcolor","255 255 255")
		smoke:SetKeyValue("opacity","5")
		smoke:SetKeyValue("spawnrate","60")
		smoke:SetKeyValue("lifetime","7")
		smoke:SetPos(self.Entity:GetPos())
		smoke:Spawn()
		smoke:Fire("kill","",10)
		smoke:SetParent(self.Entity)

		local light = ents.Create("env_lightglow") //Nice little glow from the rocket flame
		light:SetPos(self.Entity:GetPos())
		light:SetKeyValue("targetname", "moo")
		light:SetKeyValue("rendercolor", "255 255 255")
		light:SetKeyValue("VerticalGlowSize", "10")
		light:SetKeyValue("HorizontalGlowSize", "10")
		light:Spawn()
		light:SetParent(self.Entity)

		self.PreLaunch = true
	end

	self.XCo = self.ParL.XCo
	self.YCo = self.ParL.YCo
	self.ZCo = self.ParL.ZCo
	
	if (self.ParL.Locked == true) then
		self.Locked = true
	end
	if (CurTime() > self.LTime) then
		self.Locked = true
	end

end

function ENT:HitEffect()
	for k, v in pairs ( ents.FindInSphere( self.Entity:GetPos(), 500 ) ) do
		if v:IsValid() && v:IsPlayer() then
		
		v:ConCommand( "pp_motionblur 1; pp_bloom 1; pp_dof 1" )
		timer.Simple( 8, v.ConCommand, v, "pp_motionblur 0; pp_bloom 0; pp_dof 0" )

		end	
	end
end

function ENT:PhysicsCollide( data, physobj )

	if(!self.Exploded) then
		local expl=ents.Create("env_explosion") -- The "Boom" Part
		expl:SetPos(self.Entity:GetPos())
		expl:SetName("Missile")
		expl:SetParent(self.Entity)
		expl:SetOwner(self.Entity:GetOwner())
		expl:SetKeyValue("iMagnitude","300");
		expl:SetKeyValue("iRadiusOverride", 250)
		expl:Spawn()
		expl:Activate()
		expl:Fire("explode", "", 0)
		expl:Fire("kill","",0)
		self.Exploded = true

	local effectdata = EffectData() -- Credits to Pac_187 for the Effect(s)
		effectdata:SetOrigin( self.Entity:GetPos() )
	util.Effect( "Explosion", effectdata )			 -- Explosion effect
	util.Effect( "HelicopterMegaBomb", effectdata )	 -- Big flame
	util.Effect( "Rocket_Explosion", effectdata )	 -- self made effect 

        	self.shakeeffect = ents.Create("env_shake") -- Shake from the explosion
         	self.shakeeffect:SetKeyValue("amplitude","16")
        	self.shakeeffect:SetKeyValue("spawnflags","29")
        	self.shakeeffect:SetKeyValue("frequency","200.0")
        	self.shakeeffect:SetKeyValue("duration","2")
        	self.shakeeffect:SetPos(self.Entity:GetPos())
        	self.shakeeffect:Fire("StartShake","",0)
         	self.shakeeffect:Fire("Kill","",4)

		local ar2Explo = ents.Create( "env_ar2explosion" )
		ar2Explo:SetOwner( self.Owner )
		ar2Explo:SetPos( self.Entity:GetPos() )
		ar2Explo:Spawn()
		ar2Explo:Activate()
		ar2Explo:Fire( "Explode", "", 0 )
		ar2Explo:Fire( "Kill", "", 4 )

		local physExplo = ents.Create( "env_physexplosion" )
	  	physExplo:SetOwner( self.Owner )
       	 	physExplo:SetPos( self.Entity:GetPos() )
        	physExplo:SetKeyValue( "Magnitude", "300" )	-- Power of the Physicsexplosion
        	physExplo:SetKeyValue( "radius", "500" )	-- Radius of the explosion
        	physExplo:SetKeyValue( "spawnflags", "19" )
        	physExplo:Spawn()
        	physExplo:Fire( "Explode", "", 0 )
        	physExplo:Fire( "Kill", "", 2 )


	for k, v in pairs ( ents.FindInSphere( self.Entity:GetPos(), 350 ) ) do
		v:Ignite( 10, 0 )

	end
end

function ENT:OnTakeDamage( dmginfo )

	if(!self.Exploded) then
		local expl=ents.Create("env_explosion")
		expl:SetPos(self.Entity:GetPos())
		expl:SetName("Missile")
		expl:SetParent(self.Entity)
		expl:SetOwner(self.Entity:GetOwner())
		expl:SetKeyValue("iMagnitude","300");
		expl:SetKeyValue("iRadiusOverride", 250)
		expl:Spawn()
		expl:Activate()
		expl:Fire("explode", "", 0)
		expl:Fire("kill","",0)
		self.Exploded = true
	end	
end

function ENT:Use( activator, caller )

	end
end